<?php

namespace App\Service\Kms\System;

use App\Enums\OpenShop\InvestmentPromotion\OrganizationManageEnum;
use App\Error\Kms\CameraError;
use App\Models\OpenShop\InvestmentPromotion\OpenShopOrganizationManageModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopOrganizationManageUserModel;
use App\Service\Kms\Common\DateService;
use App\Service\Kms\Common\WeatherService;
use App\Models\Kms\Album\AlbumResourceModel;
use App\Service\Kms\KmsBaseService;
use App\Service\Kms\Poi\PoiService;
use App\Service\Kms\Task\UserRoleService;
use App\Service\Kms\User\UserService;
use App\Service\OpenShop\Feishu\ConfigService;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Validation\ValidationException;

class SystemService extends KmsBaseService
{
    /**
     * @param object $params
     * @return array|void
     * @throws GuzzleException
     * @throws Exception
     */
    public static function watermark(object $params)
    {
        try {
            $recommendWatermark = ConfigService::getConfigDetail("kms.album", "watermark", 60 * 60)["value"] ?? [];
            $kfPoi              = new \stdClass();
            $aMapPoi            = new \stdClass();
            if ($params->withRecommend) {
                $kfPoi = PoiService::list((object)[
                    "lng"    => $params->long,
                    "lat"    => $params->lat,
                    "radius" => $recommendWatermark["radius"],
                    "unit"   => $recommendWatermark["unit"]
                ]);

//                $kfPoi["isEmpty"] = true;
                if ($kfPoi["isEmpty"]) {
                    $aMapPoi = attributeGet(PoiService::aMapList((object)[
                        "lng"    => $params->long,
                        "lat"    => $params->lat,
                        "radius" => $recommendWatermark["radius"],
                        "unit"   => $recommendWatermark["unit"],
                        "type"   => ""
                    ], 1, 1), "aMapPoi.list.0", new \stdClass());
                }
            }

            return array_merge($recommendWatermark, [
                "weather"  => WeatherService::getWeatherInfo($params->long, $params->lat),
                "kfPoi"    => is_object($kfPoi) ? $kfPoi : collect($kfPoi)->only([
                    "wgPoi",
                    "isEmpty",
                    AlbumResourceModel::POI_TYPE_WG_KF_SHOP,
                    AlbumResourceModel::POI_TYPE_WG_COMPLEMENT_GOODS,
                    AlbumResourceModel::POI_TYPE_WG_COMPETING_PRODUCTS
                ])->toArray(),
                "aMapPoi"  => $aMapPoi,
                "userinfo" => attributeGet(UserService::userinfo(), "userinfo", []),
                "time"     => DateService::translateDate(time())
            ]);
        } catch (Exception $exception) {
            handleKfException($exception, CameraError::GET_WATERMARK_FAIL, func_get_args());
        }
    }

    const PUNCH_TYPE_1    = 1;
    const PUNCH_TYPE_2    = 2;
    const PUNCH_TYPE_3    = 3;
    const PUNCH_TYPE_4    = 4;
    const PUNCH_TYPE_TEXT = [
        self::PUNCH_TYPE_1 => "看点巡铺",
        self::PUNCH_TYPE_2 => "寻店帮扶",
        self::PUNCH_TYPE_3 => "拜访沟通",
        self::PUNCH_TYPE_4 => "出发",
    ];

    /**
     * @return array[]
     */
    public static function config(): array
    {
        $fieldConfig = [
            "punchShareType" => enumToSelection(self::PUNCH_TYPE_TEXT)
        ];
        return [
            "field" => $fieldConfig
        ];
    }


    /**
     * @return array[]
     * @throws ValidationException
     */
    public static function loginIndexTabAuth(): array
    {
        $isAudit               = false;
        $userRole              = UserRoleService::getUserRole(auth()->user()->rmsUserId);
        $allOrganizationManage = OpenShopOrganizationManageModel::allOrganizationManage();
        $allOrganizationManage->each(
            function (OpenShopOrganizationManageModel $organizationManageModel) use (&$isAudit) {
                $exists = $organizationManageModel->managerUserRel->where("userId", auth()->user()->rmsUserId)
                                                                  ->whereIn("type", [
                                                                      OpenShopOrganizationManageUserModel::TYPE_CHECK_MANAGER,
                                                                  ])->count();
                if ($exists) {
                    $isAudit = true;
                    return false;
                }
            }
        );

        return [
            "homeTab"         => in_array($userRole["role"], [
                UserRoleService::ROLE_SPECIAL_0,
                UserRoleService::ROLE_SPECIAL_1,
                UserRoleService::ROLE_SPECIAL_2,
                UserRoleService::ROLE_BASE_PROVINCE,
                UserRoleService::ROLE_BASE_CITY_MANAGER,
                UserRoleService::ROLE_BASE_DEVELOP_CITY_MANAGER,
                UserRoleService::ROLE_BASE_SUPERVISOR,
                UserRoleService::ROLE_BASE_OBSERVER,
                UserRoleService::ROLE_BASE_WAR_REGION,
            ]),
            "shopTab"         => in_array($userRole["role"], [
                UserRoleService::ROLE_SPECIAL_0,
                UserRoleService::ROLE_SPECIAL_1,
                UserRoleService::ROLE_SPECIAL_2,
                UserRoleService::ROLE_BASE_PROVINCE,
                UserRoleService::ROLE_BASE_CITY_MANAGER,
                UserRoleService::ROLE_BASE_DEVELOP_CITY_MANAGER,
                UserRoleService::ROLE_BASE_SUPERVISOR,
                UserRoleService::ROLE_BASE_OBSERVER,
                UserRoleService::ROLE_BASE_PREPARE,
                UserRoleService::ROLE_BASE_WAR_REGION
            ]),
            "siteLocationTab" => $userRole["role"] != UserRoleService::ROLE_SPECIAL_NO_ACCESS && !$isAudit && !in_array(
                    $userRole["role"],
                    [
                        UserRoleService::ROLE_BASE_PREPARE,
                        UserRoleService::ROLE_BASE_SUPERVISOR,
                        UserRoleService::ROLE_SPECIAL_3,
                        UserRoleService::ROLE_SPECIAL_4
                    ]
                ),
            "taskTab"         => $userRole["role"] != UserRoleService::ROLE_SPECIAL_NO_ACCESS && !$isAudit,
            "mineTab"         => true,
            // todo 首页风云榜权限
            "billboardBtn"    => in_array($userRole["role"], [
                UserRoleService::ROLE_SPECIAL_0,
                UserRoleService::ROLE_SPECIAL_1,
                UserRoleService::ROLE_SPECIAL_2,
                UserRoleService::ROLE_BASE_PROVINCE,
                UserRoleService::ROLE_BASE_CITY_MANAGER,
                UserRoleService::ROLE_BASE_DEVELOP_CITY_MANAGER,
                UserRoleService::ROLE_BASE_OBSERVER,
                UserRoleService::ROLE_BASE_WAR_REGION
            ])
        ];
    }
}
